package com.bmft.jingdong.service;

import com.bmft.jingdong.constant.IndexConst;
import com.bmft.jingdong.pojo.GoodContent;
import com.bmft.jingdong.util.es.ElasticSearchApi;
import com.bmft.jingdong.util.jsoup.CrawlerUtil;
import lombok.extern.log4j.Log4j2;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Size;
import javax.xml.ws.ServiceMode;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Jarvan
 * @version 1.0
 * @create 2020/11/11 18:45
 */
@Service
@Log4j2
public class IndexService {
    @Autowired
    ElasticSearchApi elasticSearchApi;

    /**
     * 返回goodsContent 数据，如果es里面没有就爬虫并返回并添加到es搜索引擎中。
     */
    public List<Map<String, Object>> getContentList(String SearchKey,int from ,int size) throws Exception {
        List<Map<String, Object>> list = elasticSearchApi.getList(SearchKey, from, size);
        if (list.size() < IndexConst.MIN_SIZE_OF_LIST){
            boolean b = elasticSearchApi.addList(CrawlerUtil.getGoodsContent(SearchKey));
            log.info("=======是否添加数据成功:"+b+" ===========");
            return elasticSearchApi.getList(SearchKey, from, size);
        }
        return list;
    }
}
